<template>
  <orh-table add="add" pages @on-add="displays" :pageDate="pageDate" @on-page-change="queryData" del="del" @on-del="confirm">
        <Table :columns="columns1" :data="tealeaveList" slot="table" @on-selection-change="selectOne" :loading="tableload"  highlight-row ref="currentRowTable"></Table>
        <orh-sidebar v-model="show" :title="layouttitle" scale slot="others">
            <component :is="content" @requery="requery" :infoId='infoId' :data="data"></component>
        </orh-sidebar>
    </orh-table>
</template>

<script>
export default {
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);}, 
		MyLeaveDetail: resolve => {require(['./MyLeaveDetail.vue'], resolve);}, 
    },
    name: 'leave-myleave',
        data(){
            return {
                content:null,
                data:null,
                del:[],
                pageDate: {
                    total: 0,
                    current_page: 1,
                    per_page: 1
                },
                show: false,
                layouttitle:null,
                tealeaveList:[],
                studentList:[],                
                infoId: null,
                tableload:true,
                listurl:'api/leave/v1/teacher_leave/my_leave?page=',
                delUrl:'api/leave/v1/teacher_leave/delete/',
                columns1: [
                   {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                    },
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '教师姓名',
                        render: (h, params) => {
                            return h('div', params.row.teacher!=null?params.row.teacher.teacher_name.zh:'');
                        }
                    },
                    {
                        title: '请假种类',   
                        render: (h, params) => {
                            return h('div', params.row.teacher_leave_kinds.length!= 0?params.row.teacher_leave_kinds[0].dictionary_name.zh:'');
                        }
                    },
                    {
                        title: '请假开始时间',       
                        align: 'center',
                        key:'start_time',
                    },
                    {
                        title: '请假结束时间',       
                        align: 'center',
                        key:'end_time',
                    },
                    {
                        title: '是否调课',
                        align: 'center',      
                        render: (h, params) => {
                            return h('div', [
                                h('div',params.row.whether_class == '1'?'是':'否')
                            ]);
                        }
                    },
                    {
                        title: '审核状态',       
                        align: 'center',
                        render: (h, params) => {
                        let col='';
                        let name='';
                        if(params.row.examine_status== '0'){
                            col='#007d7f';
                            name='未审核';
                        }else if(params.row.examine_status== '1'){
                            col='#1aad33';
                            name='通过';
                        }else if(params.row.examine_status== '2'){
                            col='#f00';
                            name='未通过';
                        }else{
                            col='#FFC107';
                            name='审核中';
                        }
                        return h('div', {
                            style: {
                            color: col,
                            },
                        }, name)
                        }
                    },  
                    {
                        title: '操作',
                        key: 'action',
                        width: 200,
                        align: 'center',
                        render: (h, params) => {
                        let statu = params.row.examine_status
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'success',
                                        size: 'small',
                                        
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.see(params.row)
                                        }
                                    }
                                }, '查看'),
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'edit',
                                        }
                                    ],
                                    props: {
                                        type: 'primary',
                                        size: 'small',
                                        disabled:statu ==0 || statu ==2?false:true
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.update(params.row.teacher_leave_id)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    directives: [
                                        {
                                            name: "button",
                                            value: "del"
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small',
                                        disabled:statu ==0 || statu ==2?false:true
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.del = [params.row.teacher_leave_id]
                                            this.confirm()
                                        }
                                    }
                                }, '删除'),
                            ]); 
                        },
                    }
                ],               
            }
        },
        methods: {           
            // 获取教师请假的数据
            queryData(index){
                this.tableload = true
                this.$http.get(`${this.listurl}${index}`).then(val=>{
                    this.tealeaveList = val.data.leave.data
                    this.pageDate.total=val.data.leave.total
                    this.pageDate.per_page=val.data.leave.per_page
                    this.pageDate.current_page=val.data.leave.current_page
                    this.tableload = false  
                }).catch(err => {
                    if (err.code && err.code == 422) {
                        this.$Message.error(err.message);
                    }
                    this.tableload = false
                })
            },
            //添加
            displays(){
                this.show = true
                this.content = 'Operation'
                this.infoId = null
                this.layouttitle='申请'
            },
            // 点击编辑时向子组件传数据 
            update(index){
                this.infoId = index
                this.show = true
			    this.content = 'Operation'
                this.layouttitle='修改'
            },   
            //查看 
            see(data){	
                this.show = true
			    this.content = 'MyLeaveDetail'
                this.data= data
                this.layouttitle= '查看详情'
            },
            /************************************************************
             * 批量删除
             ************************************************************/
            selectOne(index){
                this.del = index.map(item=> item.teacher_leave_id)
            },
            /************************************************************
             * 执行删除操作
             ************************************************************/
            confirm () {
                this.$Modal.confirm({
                    title: '数据删除',
                    content: '<p>您确定要删除这些数据吗?</p>',
                    onOk: () => {
                        if(this.del.length==0){
                            this.$Message.error('请选择删除项');
                            return
                        }
                        this.del = JSON.stringify(this.del)
                        this.tableload = true 
                        this.$http.delete(`${this.delUrl}${this.del}`).then(val=>{  
                            this.del=[]
                            this.queryData(this.pageDate.current_page)    
                            this.$Message.success(val.message);           
                        }).catch(err=>{
                            if(err.code && err.code==422) {
                                this.$Message.error(err.message);
                            }
                            this.del=[]
                            this.tableload = false 
                        })
                    },
                });
            },
            // 修改返回数据更新
            requery(val){            
                if(val == true){
                    this.show = false
                    this.queryData(this.pageDate.current_page);
                }
            },
        },
        mounted(){
            this.queryData(this.pageDate.current_page);
        },
}
</script>
